package com.example.ota_ble.ble.component

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.ota_ble.R
import com.example.ota_ble.ble.data.dataBlueScanItem
import kotlin.toString
@SuppressLint("MissingPermission")
open class scanListAdapter (private val bleDeviceList: List<dataBlueScanItem>) :
    RecyclerView.Adapter<scanListAdapter.BleScanViewHolder>(){

    lateinit var onListItemLister :OnScanListItemClickLister
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BleScanViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_blescan_layout, parent, false)
        return BleScanViewHolder(itemView)
    }

        override fun onBindViewHolder(
        holder: BleScanViewHolder,
        position: Int
    ) {
            val device = bleDeviceList[position]
            holder.nameTextView.text = device.dev.name
            holder.addressTextView.text = device.dev.address
            holder.rssiTextView.text = device.rssi.toString()
            holder.ble_versionTextView.text = device.version[0].toString()
            holder.mcu_versionTextView.text = device.version[1].toString()
            if(this.onListItemLister!=null)
            {
                holder.itemView.setOnClickListener {
                    this.onListItemLister.on_click_item(device.dev)
                }

            }

    }

    override fun getItemCount(): Int = bleDeviceList.size

    class BleScanViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val nameTextView: TextView = itemView.findViewById(R.id.ble_scan_name)
        val addressTextView: TextView = itemView.findViewById(R.id.ble_addr)
        val rssiTextView: TextView = itemView.findViewById(R.id.ble_rssi)
        val ble_versionTextView: TextView = itemView.findViewById(R.id.ble_scan_version_ble)
        val mcu_versionTextView: TextView = itemView.findViewById(R.id.ble_scan_version_mcu)
    }

    companion object{
        interface OnScanListItemClickLister{
            fun on_click_item(bluetoothDev: BluetoothDevice)
        }
    }
    fun register_ListItemClickLister(itemClickRecall:OnScanListItemClickLister)
    {
        this.onListItemLister = itemClickRecall
    }
}